// pages/deliver/deliver.js
var app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    list: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id
    })
  },
  getOrder: function () {
    var that = this
    wx.request({
      url: app.globalData.api + 'warehouse/' + this.data.id,
      data: {},
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success: (res) => {
        console.log(res)
        if (res.data.code == 200) {
          that.data.list = res.data.data
          for (var i in that.data.list.warehouseItemList){
            that.data.list.warehouseItemList[i].actualQuantity = ''
          }
          that.setData({
            list: that.data.list
          })
        }
      }
    })
  },
  num: function (e) {
    // console.log(e)
    this.data.list.warehouseItemList[e.currentTarget.dataset.index].confirmQuantity = e.detail.value
  },
  sub: function () {
    for (var i in this.data.list.warehouseItemList) {
      if (this.data.list.warehouseItemList[i].confirmQuantity == '') {
        wx.showToast({
          title: '请填写签收数量',
        })
        return
      }
    }
    wx.request({
      url: app.globalData.api + 'warehouse/confirmWarehouseItem/' + this.data.id,
      data: {
        businessOrderFormId: this.data.list.businessOrderFormId,
        productionOrderFormId: this.data.list.productionOrderFormId,
        warehouseItemList: this.data.list.warehouseItemList,
      },
      header: {
        Authorization: wx.getStorageSync('token')
      },
      method: 'PUT',
      success: (res) => {
        console.log(res)
        if (res.data.code == 200) {
          wx.showToast({
            title: res.data.msg,
          })
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            })
          }, 1000)
        } else {
          wx.showToast({
            icon: 'none',
            title: res.data.errorMsg,
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getOrder()

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})